package ClassData;

import java.nio.ByteBuffer;

import ClassData.Visitor.IVisitor;
import ClassData.Visitor.IVisitorElement;

/**
 * @note changed by Dominik Ollesz
 */
public class AttributeInfo_Code extends AttributeInfo implements IVisitorElement {

	AttributeInfo []  myAttributes; 

	short max_stack;
	short max_locals;

	byte [] code;
	
	short exceptionHandlers;
	ExceptionHandler [] exceptionHandler;
	AttributeInfo [] myAttributers;
		
	public AttributeInfo_Code(ByteBuffer dataBuffer,ConstantPool constantPool) {
		int code_length;
		short exceptionCounter;
		short attributeCounter;
		
		max_stack=dataBuffer.getShort();
		max_locals=dataBuffer.getShort();
		code_length=dataBuffer.getInt();
		
		code=new byte [code_length];
		dataBuffer.get(code, 0, code.length);
		
		exceptionHandlers = dataBuffer.getShort();
		exceptionHandler=new ExceptionHandler[exceptionHandlers];
		
		for (exceptionCounter=0;exceptionCounter<exceptionHandler.length ;exceptionCounter++) {
			exceptionHandler[exceptionCounter] = new ExceptionHandler(dataBuffer.getShort(),dataBuffer.getShort(),dataBuffer.getShort(),dataBuffer.getShort());
		}
		
		myAttributes = new AttributeInfo[dataBuffer.getShort()]; 
		
		for (attributeCounter=0;attributeCounter<myAttributes.length;attributeCounter++) {
			myAttributes[attributeCounter]=parseAttribute(dataBuffer,constantPool);
		}
	}

	public int getCodeSize() {
		return(code.length);	
	}
	
	public int getMaxStack() {
		return(max_stack);
	}
	
	
	public int getMaxLocals() {
		return(max_locals);
	}
	
	public byte[] getCode() {
		return(code);
	}
	
	/**
	 * @note author Dominik Ollesz
	 */
	public int getExceptionHandlers() {
		return(exceptionHandlers);
	}

	/**
	 * @note added by Dominik Ollesz
	 */
	public ExceptionHandler[] getExceptionHandler() {
		return exceptionHandler;
	}
	
	@Override
	public void receive_Visitor(IVisitor visitor) {
		visitor.visit_AttributeInfo_Code(this);
	}
	
}